/*
** Mortem's HGE Addon 1.1
** Copyright (C) 2006-2007, Osokin 'elmortem' Makar
** http://elmortem.mirgames.ru
**
** 2D camera.
*/

#include "mhacamera.h"

HGE *mhaCamera::hge = 0;

mhaCamera::mhaCamera(float _width, float _height, float _speed) {
	hge = hgeCreate(HGE_VERSION);
	
	width = _width;
	height = _height;
	speed = _speed;
	Reset();
}

mhaCamera::~mhaCamera(void) {
	hge->Release();
}

void mhaCamera::Reset(void) {
	pos.Set(0, 0);
	dpos.Set(0, 0);
}

void mhaCamera::Set(float _x, float _y) {
	mhaVector v(_x, _y);
	Set(v);
}

void mhaCamera::Set(mhaVector _pos) {
	dpos = _pos;
	if(dpos.x < 0) dpos.x = 0;
	if(dpos.x > width) dpos.x = width;
	if(dpos.y < 0) dpos.y = 0;
	if(dpos.y > height) dpos.y = height;
}

void mhaCamera::Move(float _x, float _y) {
	mhaVector v(_x, _y);
	Move(v);
}

void mhaCamera::Move(mhaVector _pos) {
	dpos.x += _pos.x;
	dpos.y += _pos.y;
	Set(dpos);
}

void mhaCamera::Update(float delta) {
	float dist = pos.DistanceTo(dpos);
	if(dist > 1.f) dist = dist * speed * delta;
	pos.Move(dist, pos.AngleTo(dpos));
	if(dist < 1.f) dpos = pos;

	if(pos.x < 0) {
    pos.x = 0;
    dpos.x = 0;
	}
	if(pos.x > width) {
    pos.x = width;
    dpos.x = width;
	}
	if(pos.y < 0) {
    pos.y = 0;
    dpos.y = 0;
	}
	if(pos.y > height) {
    pos.y = height;
    dpos.y = height;
	}
}

void mhaCamera::Apply(void) {
	hge->Gfx_SetTransform(-pos.x, -pos.y, -pos.x, -pos.y, 0, 1, 1);
}
